<template>
  <div>
    <el-dialog
      v-model="props.visible"
      title="生产单列表"
      width="80%"
      @close="handleCancel"
    >
      <el-form
        :model="queryParams"
        class="flex-box"
        ref="queryRef"
        :inline="true"
      >
        <div>
          <el-form-item label="生产单编码" prop="produceNo">
            <el-input
              v-model.trim="queryParams.produceNo"
              placeholder="请输入生产单编码"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">
              搜索
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
        <el-button
          type="primary"
          plain
          @click="handleSubmit"
          :disabled="multiple"
        >
          确认
        </el-button>
      </el-form>
      <el-row :gutter="20" style="overflow: auto">
        <el-col :span="8">
          <el-table
            v-loading="loading"
            :data="orderList"
            stripe
            border
            style="cursor: pointer"
            @cell-click="handleClick"
            ref="orderListTable"
            highlight-current-row
          >
            <el-table-column
              label="序号"
              width="55"
              type="index"
              align="center"
            />
            <el-table-column
              label="生产单编码"
              align="center"
              prop="produceNo"
            ></el-table-column>
            <el-table-column label="下单日期" align="center" prop="submitDate">
              <template v-slot="{ row }">
                {{ row.submitDate ? row.submitDate.split("T")[0] : "" }}
              </template>
            </el-table-column>
            <el-table-column label="类型" align="center" prop="type">
              <template v-slot="{ row }">
                <el-tag :type="row.type == 1 ? 'success' : ''" effect="dark">
                  {{ row.type == 1 ? "封面" : "内文" }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="16">
          <el-table
            ref="orderRef"
            v-loading="loadingInfo"
            :data="bindList"
            row-key="productId"
            stripe
            border
            style="cursor: pointer"
            height="420px"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              :reserve-selection="true"
              width="55"
            />
            <el-table-column
              label="序号"
              width="55"
              type="index"
              align="center"
            />
            <el-table-column
              label="产品名称"
              prop="productName"
              min-width="220"
              align="center"
            ></el-table-column>
            <el-table-column
              label="品种"
              prop="breed"
              min-width="160"
              align="center"
            ></el-table-column>
            <!-- <el-table-column
                            label="印刷方式"
                            align="center"
                            :show-overflow-tooltip="true"
                            prop="printType"
                        >
                            <template #default="scope">
                                <dict-tag
                                    :options="mes_paper_package"
                                    :value="scope.row.printType"
                                />
                            </template>
                        </el-table-column> -->
            <el-table-column
              label="装订方式"
              min-width="160"
              prop="bindingMode"
              align="center"
            >
              <template #default="scope">
                <dict-tag
                  :options="ems_order_binding_mode"
                  :value="scope.row.bindingMode"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="开本数"
              min-width="160"
              prop="bookSize"
              align="center"
            >
              <template #default="scope">
                <dict-tag
                  :options="ems_produce_book_size"
                  :value="scope.row.bookSize"
                />
              </template>
            </el-table-column>
            <el-table-column label="印色" align="center" prop="printColor">
              <template #default="scope">
                <dict-tag
                  :options="ems_order_print_color"
                  :value="scope.row.printColor"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="印张"
              align="center"
              prop="sheet"
            ></el-table-column>
            <!-- <el-table-column
                            label="贴标"
                            align="center"
                            :show-overflow-tooltip="true"
                            prop="pieceView"
                        >
                        </el-table-column> -->
            <!-- <el-table-column
                            label="印数"
                            align="center"
                            :show-overflow-tooltip="true"
                            prop="printAmount"
                        ></el-table-column> -->
          </el-table>
        </el-col>
      </el-row>
      <div class="footer">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { toNumber } from "@/utils/ruoyi";
import {
  GetProducelistByMachineId,
  getByIdFilter,
  getByIdFilterBind,
} from "@/api/production/cover";
const emit = defineEmits(["changeUpdate", "cancel"]);
const { proxy } = getCurrentInstance();
const {
  mes_paper_package,
  ems_order_print_color,
  ems_produce_book_size,
  ems_order_binding_mode,
} = proxy.useDict(
  "mes_paper_package",
  "ems_order_print_color",
  "ems_produce_book_size",
  "ems_order_binding_mode"
);

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  orderSelectList: {
    type: Array,
    default: () => {
      [];
    },
  },
  params: {
    type: Object,
    default: () => {
      {
      }
    },
  },
  bindingType: {
    type: String,
    default: "",
  },
  workshopName: {
    type: String,
    default: "",
  },
});

const orderRef = ref(null);
const orderList = ref([]);
const bindList = ref([]);
const multiple = ref(true);
const selectRow = ref([]);
const loading = ref(false);
const loadingInfo = ref(false);
const orderListTable = ref(null);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  type: 2,
  pubNo:null,
  produceNo:null,
  status: "5,6,7,8,9,10,11,12,13,14,15,16,17,18",
});
watch(
  () => props.visible,
  (val) => {
    if (val) {
      handleQuery();
      nextTick(() => {
        orderRef.value.clearSelection();
        if (props.orderSelectList.length > 0) {
          props.orderSelectList.forEach((item) => {
            orderRef.value.toggleRowSelection(item, true);
          });
        }
      });
    }
  }
);
const total = ref(0);
// 选中订单
const handleClick = (row) => {
  loadingInfo.value = true;
  let bindingType = undefined;
  if (props.bindingType) {
    bindingType = props.bindingType;
  }
  getByIdFilterBind({
    bindingType,
    produceId: row.produceId,
    machineId: props.params.machineId,
  }).then((res) => {
    res.data.produceProductList.forEach((item) => {
      item.productName = res.data.productName
      /****
       * 如果0.5<印张<10,计件工资=0.024
       * ，如果10<=印张<16，计件工资=0.034，
       * 如果印刷方式是卷筒且16<=印张<200，计件工资=0.04；
       * 如果印刷方式是平张且装订车间是二车间且印张>=22，计件工资=0.06，
       * 如果印刷方式是平张且装订车间是三车间且印张>=26，计件工资=0.06，
       */

      if (item.sheet < 10) {
        item.pieceStandard = 0.024;
      } else if (item.sheet < 16) {
        item.pieceStandard = 0.034;
      } else if (item.sheet < 200) {
        item.pieceStandard = 0.04;
      }

      // if (item.produceCount < 3000) {
      //   item.completeCount = 3000;
      // }
      res.data.producePrintList.forEach((obj) => {
        if (item.orderProductId == obj.orderProductId) {
          if (
            obj.printType == 1 &&
            props.workshopName == "二车间" &&
            item.sheet >= 22
          ) {
            item.pieceStandard = 0.06;
          }
          if (
            obj.printType == 1 &&
            props.workshopName == "三车间" &&
            item.sheet >= 26
          ) {
            item.pieceStandard = 0.06;
          }
        }
      });
      item.produceNo = row.produceNo;
      item.orderDate = row.submitDate;
      item.orderCount = item.produceCount;
    });
    // bindList.value = res.data.produceProductList.filter(
    //     (item) => item.breed != "目录"
    //   );
    if (props.bindingType) {
      if (props.bindingType == 1) {
        bindList.value = res.data.produceProductList.filter(
          (item) => Number(item.sheet) <= 18
        );
      } else {
        bindList.value = res.data.produceProductList.filter(
          (item) => Number(item.sheet) > 18
        );
      }
    } else {
      bindList.value = res.data.produceProductList.filter(
        (item) => item.breed != "目录"
      );
    }

    loadingInfo.value = false;
  });
};

// 选中
const handleSelectionChange = (row) => {
  selectRow.value = row;

  multiple.value = !row.length;
};
const handleSubmit = () => {
  selectRow.value.forEach((item) => {
    // if (item.completeCount < 3000) {
    //   item.pieceMoney = toNumber(
    //     Number(item.completeCount) * Number(item.pieceStandard || 0),
    //     2
    //   );
    // }

    // 骑订 工价 0.012
    if (
      props.params.machineId == "c08bd7a574db4ff49ed84729649d3d93" ||
      props.params.machineId == "1a525d51f60149809f7c9a31cac257c1" ||
      props.params.machineId == "b9058bd0e42046eab97eb275edb7fc2c" ||
      props.params.machineId == "9f1f578a4a70485e925148443ba1e6b6" ||
      props.params.machineId == "efc3b305c9c945fb81f29afe02940c31"
    ) {
      item.pieceStandard = 0.012;
    }

    if (props.bindingType) {
      if (props.bindingType == 3) {
        item.pieceStandard = 0.05;
      } else {
        item.pieceStandard = 0.045;
      }
    }
  });
  emit("changeUpdate", selectRow.value);
};
const handleCancel = () => {
  bindList.value = [];
  proxy.resetForm("queryRef");
  emit("cancel");
};
const getList = async () => {
  loading.value = true;
  const res = await GetProducelistByMachineId({
    ...queryParams.value,
    ...props.params,
  });
  orderListTable.value.setCurrentRow(res.rows[0]);
  loading.value = false;
  orderList.value = res.rows;
  if (orderList.value.length > 0) {
    handleClick(orderList.value[0]);
  } else {
    bindList.value = [];
  }
  total.value = res.total;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// getList()
</script>

<style lang="scss" scoped></style>
